﻿@{
    ViewData["Title"] = "Home Page";
}
@model deviceManage.Models.Message


  

        <div class="layui-fluid" id="LAY-app-message">
            <div class="layui-card">
                <div class="layui-tab layui-tab-brief">
                    <ul class="layui-tab-title">
                        <li class="layui-this">全部消息</li>
                        <li>提醒<span class="layui-badge" id="tipCount"></span></li>
                        <li>预警<span class="layui-badge" id="warnCount"></span></li>
                    </ul>
                    <div class="layui-tab-content">

                        <div class="layui-tab-item layui-show">
                            <div class="LAY-app-message-btns" style="margin-bottom: 10px;">
                                <button class="layui-btn  layui-btn-sm" data-type="all" data-events="del">删除</button>
                                <button class="layui-btn   layui-btn-sm" data-type="all" data-events="ready">标记已读</button>
                                <button class="layui-btn  layui-btn-sm" data-type="all" data-events="readyAll">全部未读</button>
                            </div>

                            <table id="LAY-app-message-all" lay-filter="LAY-app-message-all"></table>
                        </div>
                        <div class="layui-tab-item" style="">

                            <div class="LAY-app-message-btns" style="margin-bottom: 10px;">
                                <button class="layui-btn layui-btn-primary layui-btn-sm" data-type="notice" data-events="del">删除</button>
                                <button class="layui-btn layui-btn-primary layui-btn-sm" data-type="notice" data-events="ready">标记已读</button>
                                <button class="layui-btn layui-btn-primary layui-btn-sm" data-type="notice" data-events="readyAll">全部未读</button>
                            </div>

                            <table id="LAY-app-message-notice" lay-filter="LAY-app-message-notice"></table>
                        </div>
                        <div class="layui-tab-item" style="">

                            <div class="LAY-app-message-btns" style="margin-bottom: 10px;">
                                <button class="layui-btn layui-btn-primary layui-btn-sm" data-type="direct" data-events="del">删除</button>
                                <button class="layui-btn layui-btn-primary layui-btn-sm" data-type="direct" data-events="ready">标记已读</button>
                                <button class="layui-btn layui-btn-primary layui-btn-sm" data-type="direct" data-events="readyAll">全部未读</button>
                            </div>

                            <table id="LAY-app-message-direct" lay-filter="LAY-app-message-direct"></table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
 

<script>
       


        var DISABLED = 'layui-btn-disabled'

            //区分各选项卡中的表格
            , tabs = {
                all: {
                    text: '全部消息'
                    , id: 'LAY-app-message-all'
                }
                , notice: {
                    text: '通知'
                    , id: 'LAY-app-message-notice'
                }
                , direct: {
                    text: '预警'
                    , id: 'LAY-app-message-direct'
                }
            };

        //标题内容模板
        var tplTitle = function (d) {
            return '<a lay-href="app/message/detail/id=' + d.id + '">' + d.title;
        };

        //全部消息
    var messageTableObj = table.render({
            elem: '#LAY-app-message-all'
            , url: './allMsg'  
            , height: 'full-150'
            , page: true
            , cols: [[
                { type: 'checkbox', fixed: 'left' }
                , { field: 'title', title: '标题内容', minWidth: 100, templet: tplTitle }
                , { field: 'content', title: '内容', minWidth: 300, templet: '<div>{{ d.content }}</div>' }
                , { field: 'createDate', title: '时间', width: 170, templet: '<div>{{ d.createDate}}</div>' }
            ]]
            , skin: 'line'
        });

        //提醒
    var noticeTableObj = table.render({
            elem: '#LAY-app-message-notice'
        , url: './allTipMsg'  
        , height: 'full-150'
            , page: true
            , cols: [[
                { type: 'checkbox', fixed: 'left' }
                , { field: 'title', title: '标题内容', minWidth: 100, templet: tplTitle }
                , { field: 'content', title: '内容', minWidth: 300, templet: '<div>{{ d.content }}</div>' }
                , { field: 'createDate', title: '时间', width: 170, templet: '<div>{{ layui.util.timeAgo(d.createDate) }}</div>' }
            ]]
            , skin: 'line'
        });

        //预警
    var warnTableObj = table.render({
            elem: '#LAY-app-message-direct'
        , url: './allWarnMsg'  
        , height: 'full-150'
            , page: true
            , cols: [[
                { type: 'checkbox', fixed: 'left' }
                , { field: 'title', title: '标题内容', minWidth: 100, templet: tplTitle }
                , { field: 'content', title: '内容', minWidth: 300, templet: '<div>{{ d.content }}</div>' }
                , { field: 'time', title: '时间', width: 170, templet: '<div>{{ layui.util.timeAgo(d.time) }}</div>' }
            ]]
            , skin: 'line'
        });

        //事件处理
        var events = {
            del: function (othis, type) {
                var thisTabs = tabs[type]
                    , checkStatus = table.checkStatus(thisTabs.id)
                    , data = checkStatus.data; //获得选中的数据
                if (data.length === 0) return layer.msg('未选中行');
             
                layer.confirm('确定删除选中的数据吗？', function () {
                    var ids = new Array();
                    for (var i=0;i<data.length;i++) {
                        var obj = data[i];
                        ids.push(obj.id);
                    }
                    $.post("/message/doDel",    
                        { "ids": ids},
                        function (d) {
                            
                            if (d.code == 1) {
                                layer.msg(d.msg);
                                messageTableObj.reload();
                                warnTableObj.reload();
                                noticeTableObj.reload();
                                reloadCount();
                            }
                        });
                });
            }
            , ready: function (othis, type) {
                var thisTabs = tabs[type]
                    , checkStatus = table.checkStatus(thisTabs.id)
                    , data = checkStatus.data; //获得选中的数据
                if (data.length === 0) return layer.msg('未选中行');
  

                var ids = new Array();
                for (var i = 0; i < data.length; i++) {
                    var obj = data[i];
                    ids.push(obj.id);
                }
                $.post("/message/doRead",
                    { "ids": ids },
                    function (d) {

                        if (d.code == 1) {
                            layer.msg('标记已读成功', {
                                icon: 1
                            });
                            reloadCount();
                        }
                    });



            }
            , readyAll: function (othis, type) {
                var thisTabs = tabs[type]
                    , checkStatus = table.checkStatus(thisTabs.id)
                    , data = checkStatus.data; //获得选中的数据


                if (data.length === 0) return layer.msg('未选中行');


                var ids = new Array();
                for (var i = 0; i < data.length; i++) {
                    var obj = data[i];
                    ids.push(obj.id);
                }
                $.post("/message/doUnRead",
                    { "ids": ids },
                    function (d) {

                        if (d.code == 1) {
                            layer.msg('标记未读成功', {
                                icon: 1
                            });
                            reloadCount();
                        }
                    });
 
            }
        };

        $('.LAY-app-message-btns .layui-btn').on('click', function () {
            var othis = $(this)
                , thisEvent = othis.data('events')
                , type = othis.data('type');
            events[thisEvent] && events[thisEvent].call(this, othis, type);
        });

    window.onload = function () {
        reloadCount();
    } 

    function reloadCount() {
        $.ajax({
            url: "./getAllUnreadMessgeCount?type=1",
            success: function (data) {
                $("#tipCount").text(data.count);
            }
        });
        $.ajax({
            url: "./getAllUnreadMessgeCount?type=2",
            success: function (data) {
                console.log(data);
                $("#warnCount").text(data.count);
            }
        });
    }

</script>

